package com.ly.base.chapter04.framwork;

import com.zaxxer.hikari.HikariDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * todo 原始无框架(很重要)：
 *         Jdbc操作数据库
 */
public class JdbcTemplateDemo01 {
    public static void main(String[] args) {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setUsername("root");
        dataSource.setPassword("1234");
        List<Student> stuList = new ArrayList<>();
        try (Connection conn = dataSource.getConnection()) { //dataSource.getConnection() --- 建立数据库连接
            PreparedStatement stat = conn.prepareStatement("select * from student");
            ResultSet rs = stat.executeQuery();
            while (rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String sex = rs.getString("sex");
                stuList.add(new Student(id,name,sex));
            }
            for (Student stu : stuList) {
                System.out.println(stu);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


    }


    record Student(int id, String name, String sex) { }

}
